<template>
	<view class="page">
		<u-form :model="uModel" :rules="rules" ref="uForm" class="form">
			<u-form-item prop="pwd_o">
				<u-input prefix-icon="lock" v-model="uModel.pwd_o" placeholder="请输入当前密码" type="password"
					:clearable="true"></u-input>
			</u-form-item>
			<u-form-item prop="pwd_n1">
				<u-input prefix-icon="lock" v-model="uModel.pwd_n1" placeholder="请输入新密码" type="password"
					:clearable="true"></u-input>
			</u-form-item>
			<u-form-item prop="pwd_n2">
				<u-input prefix-icon="lock" v-model="uModel.pwd_n2" placeholder="请再次输入新密码" type="password"
					:clearable="true"></u-input>
			</u-form-item>
		</u-form>
		<u-button class="btn" @click="submit" type="primary" text="提交" />
	</view>
</template>

<script>
	import {
		pwd_change
	} from '@/api/common.js';
	export default {
		data() {
			return {
				uModel: {
					pwd_o: '',
					pwd_n1: '',
					pwd_n2: ''
				},
				rules: {
					pwd_o: {
						type: 'string',
						required: true,
						message: '请输入当前密码',
						trigger: ['blur']
					},
					pwd_n1: {
						type: 'string',
						required: true,
						message: '请输入新密码',
						trigger: ['blur']
					},
					pwd_n2: {
						type: 'string',
						required: true,
						message: '请再次输入新密码',
						trigger: ['blur']
					}
				}
			}
		},
		onReady() {
			//onReady 为uni-app支持的生命周期之一
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			submit() {
				console.log('确认提交:' + this.uModel.pwd_o)
				//比较两个密码、上传
				var that = this;
				if (that.uModel.pwd_o && that.uModel.pwd_n1 && that.uModel.pwd_n2) {
					if (that.uModel.pwd_n1 == that.uModel.pwd_n2) {
						pwd_change({
							oldpass: that.uModel.pwd_o,
							newpass1: that.uModel.pwd_n1,
							newpass2: that.uModel.pwd_n2
						}).then(res => {
							console.log(res);
							uni.showToast({
								title: "修改密码成功",
								icon: 'none'
							});
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/login/login'
								})
							}, 500)
						}).catch(err => {
							console.log(err);
							uni.showToast({
								title: err,
								icon: 'none'
							});
						})

					} else {
						uni.showToast({
							title: "两次密码不一致",
							icon: 'none'
						});
					}
				} else {
					uni.showToast({
						title: "密码不能为空",
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		width: 84vw;
		margin-left: 8vw;
		margin-top: 10vh;
	}

	.u-button {
		margin-top: 4vh;
	}
</style>